//
//  MqInfoSearchViewController.m
//  Meiqi
//
//  Created by TwistedFate on 16/5/25.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqInfoSearchViewController.h"
#import "MqInfoListModel.h"
#import "MqInfoSearchTableViewCell.h"
#import "MqInfoWebViewController.h"
@interface MqInfoSearchViewController ()
<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) UITextField *searchTextField;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MqInfoSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 24, kScreenWidth, 80)];
    
    bgView.image = [UIImage imageNamed:@"icon_biketoCurrency_biketoCurrencyPromote@2x"];
    [self.view addSubview:bgView];
    
    //  返回按钮
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    backButton.frame = CGRectMake(10, 45, 40, 40);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(back:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backButton];
    
    UIView *roundView = [[UIView alloc] initWithFrame:CGRectMake(backButton.right + 30, bgView.top + 15, kScreenWidth / 4 * 3, 50)];
    roundView.backgroundColor = [UIColor whiteColor];
    roundView.layer.cornerRadius = 10;
    [self.view addSubview:roundView];
    
    UIButton *searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    searchButton.backgroundColor = [UIColor blueColor];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:(UIControlStateNormal)];
    
//    [searchButton addTarget:self action:@selector(searchResults:) forControlEvents:(UIControlEventTouchUpInside)];
    searchButton.frame = CGRectMake(10, 10, 30, 30);
    [roundView addSubview:searchButton];
    
    
    _searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(searchButton.right + 10, searchButton.top, roundView.width - searchButton.right - 20, searchButton.height)];
    
    //  成为第一响应者 弹出键盘
    [_searchTextField becomeFirstResponder];
    
    [_searchTextField addTarget:self action:@selector(searchResults:) forControlEvents:(UIControlEventEditingChanged)];
    [roundView addSubview:_searchTextField];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, bgView.bottom, kScreenWidth, kScreenHeight - bgView.bottom) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 80;
    
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[MqInfoSearchTableViewCell class] forCellReuseIdentifier:@"searchTableViewCell"];
    
}

//  失去第一响应者 键盘回弹
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    return YES;
}

//  搜索textField
- (void)searchResults:(UIButton *)textField{
    
    //  初始化
    _dataArray = [NSMutableArray array];
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"page"] = @"1";
    parDic[@"keywords"] = _searchTextField.text;
    
    [PKRequestManager requestWithUrlString:@"http://www.biketo.com/app.php?m=msearch&a=search" parDic:parDic Method:POST finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        
        if ([dic[@"searchResult"] isKindOfClass:[NSString class]]) {
            
            return ;
            
        }
//
        
        NSArray *resultArray = dic[@"searchResult"];
        
        for (NSDictionary *oneDic in resultArray) {
            
            MqInfoListModel *model = [[MqInfoListModel alloc] init];
            
            [model setValuesForKeysWithDictionary:oneDic];
            
            [_dataArray addObject:model];
        }
    
        //  刷新tableView
        [_tableView reloadData];
        
        
    } error:^(NSError *error) {
        
    } path:nil];
    
}


//  返回按钮
- (void)back:(UIButton *)button{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NSLog(@"%ld",indexPath.row);
    MqInfoListModel *model = _dataArray[indexPath.row];
    
    MqInfoSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchTableViewCell" forIndexPath:indexPath];
    cell.model = model;
//    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    
    return cell;
    
}

//  cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqInfoListModel *model = _dataArray[indexPath.row];
    
    MqInfoWebViewController *webView = [[MqInfoWebViewController alloc] init];
    webView.idsURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&&classid=%@&id=%@&userid=682480&version=1",model.classid, model.topicDetailID];
    
    // 标签的网址
    webView.tagListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getArticleTagOrTagListByType&type=tagList&id=%@",model.topicDetailID];
    
    // 作者信息的网址
    webView.authorInfoURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getAuthorInfByType&type=authorInfo&classid=%@&id=%@",model.classid,model.topicDetailID];
    
    // 相关文章的网址
    webView.recommendListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&version=1&platform=1&classid=%@&showType=recommendList",model.classid];
    // 评论的网址
    webView.commentURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=comment&methodType=getArticleCommentV1&access_token=&classid=%@&id=%@",model.classid,model.topicDetailID];
    
    
    [self.navigationController pushViewController:webView animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
